//
//
//package com.ruoze.bdp.datasource.service.utils;
//
//import com.fasterxml.jackson.databind.JsonNode;
//import org.apache.dolphinscheduler.common.utils.JSONUtils;
//import org.apache.dolphinscheduler.plugin.datasource.api.datasource.BaseDataSourceParamDTO;
//import org.apache.dolphinscheduler.plugin.datasource.api.datasource.DataSourceProcessor;
//import org.apache.dolphinscheduler.plugin.datasource.api.plugin.DataSourceProcessorProvider;
//import org.apache.dolphinscheduler.spi.datasource.ConnectionParam;
//import org.apache.dolphinscheduler.spi.enums.DbType;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import java.sql.Connection;
//import java.util.Map;
//
//public class DataSourceUtils {
//
//    public DataSourceUtils() {
//    }
//
//    private static final Logger logger = LoggerFactory.getLogger(DataSourceUtils.class);
//
//    /**
//     * check datasource param
//     *
//     * @param baseDataSourceParamDTO datasource param
//     */
//    public static void checkDatasourceParam(BaseDataSourceParamDTO baseDataSourceParamDTO) {
//        getDatasourceProcessor(baseDataSourceParamDTO.getType()).checkDatasourceParam(baseDataSourceParamDTO);
//    }
//
//    /**
//     * build connection url
//     *
//     * @param baseDataSourceParamDTO datasourceParam
//     */
//    public static ConnectionParam buildConnectionParams(BaseDataSourceParamDTO baseDataSourceParamDTO) {
//        ConnectionParam connectionParams = getDatasourceProcessor(baseDataSourceParamDTO.getType())
//                .createConnectionParams(baseDataSourceParamDTO);
//        logger.info("parameters map:{}", connectionParams);
//        return connectionParams;
//    }
//
//    public static ConnectionParam buildConnectionParams(DbType dbType, String connectionJson) {
//        return getDatasourceProcessor(dbType).createConnectionParams(connectionJson);
//    }
//
//    public static String getJdbcUrl(DbType dbType, ConnectionParam baseConnectionParam) {
//        return getDatasourceProcessor(dbType).getJdbcUrl(baseConnectionParam);
//    }
//
//    public static Connection getConnection(DbType dbType, ConnectionParam connectionParam) {
//        try {
//            return getDatasourceProcessor(dbType).getConnection(connectionParam);
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//    }
//
//    public static String getDatasourceDriver(DbType dbType) {
//        return getDatasourceProcessor(dbType).getDatasourceDriver();
//    }
//
//    public static BaseDataSourceParamDTO buildDatasourceParamDTO(DbType dbType, String connectionParams) {
//        return getDatasourceProcessor(dbType).createDatasourceParamDTO(connectionParams);
//    }
//
//    public static DataSourceProcessor getDatasourceProcessor(DbType dbType) {
//        Map<String, DataSourceProcessor> dataSourceProcessorMap = DataSourceProcessorProvider.getInstance().getDataSourceProcessorMap();
//        if (!dataSourceProcessorMap.containsKey(dbType.name())) {
//            throw new IllegalArgumentException("illegal datasource type");
//        }
//        return dataSourceProcessorMap.get(dbType.name());
//    }
//
//    /**
//     * get datasource UniqueId
//     */
//    public static String getDatasourceUniqueId(ConnectionParam connectionParam, DbType dbType) {
//        return getDatasourceProcessor(dbType).getDatasourceUniqueId(connectionParam, dbType);
//    }
//
//    /**
//     * build connection url
//     */
//    public static BaseDataSourceParamDTO buildDatasourceParam(String param) {
//        JsonNode jsonNodes = JSONUtils.parseObject(param);
//
//        return getDatasourceProcessor(DbType.ofName(jsonNodes.get("type").asText().toUpperCase()))
//                .castDatasourceParamDTO(param);
//    }
//}
